package com.mokee.android.ShipPass;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MySimpleAdapter extends SimpleAdapter {
	private static final String tag = "MySimpleAdapter";

	private List<Map<String, Object>> mData;
	private LayoutInflater mInflater;
	private int mResource;
	private String[] mFrom;
	private int[] mTo;
	
	public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
		super(context, data, resource, from, to);
		this.mData = (List<Map<String, Object>>) data;
		this.mResource = resource;
		this.mFrom = from;
		this.mTo = to;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return createViewFromResource(position, convertView, parent, mResource);
	}

	private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource) {
		View v;		
        if (convertView == null) {
            v = mInflater.inflate(resource, parent, false);
        } else {
            v = convertView;
        }
		return v;
	}
	
	private void bindView(int position, View view) {
		final Map dataSet = mData.get(position);
        if (dataSet == null) {
            return;
        }

        final String[] from = mFrom;
        final int[] to = mTo;
        final int count = to.length;

        for (int i = 0; i < count; i++) {
            final TextView v = (TextView) view.findViewById(to[i]);
            if (v != null) {
                final Object data = dataSet.get(from[i]);
                String text = data == null ? "" : data.toString();
                if (text == null) {
                    text = "";
                }
                
				/*********************** 自定义的片段 ***********************/
                if(i == 1){
//                	text.equals("危险品");
                	v.setTextColor(Color.RED);
                }
                
                
                /***********************************************************/
            }
        }
	}
	
	@Override
	public int getCount() {
		return mData.size();
	}
	
	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}
}
